<script setup lang="ts">

import axios from 'axios'
import { onMounted, ref } from 'vue'
import { ChannelResData, ChannelItem } from '../types/data'
import { useChannelStore } from '../store/channel'

const list = ref<ChannelItem[]>([])

onMounted(async () => {
  const res = await axios<ChannelResData>({
    method: 'GET',
    url: 'http://geek.itheima.net/v1_0/channels'
  })
  list.value = res.data.data.channels
})

const channelStore = useChannelStore()

</script>

<template>
  <div class="channel-nav">
    <nav class="list">
      <a
        v-for="v of list"
        :key="v.id"
        class="item"
        :class="{ active: v.id === channelStore.channel_id }"
        href="javascript:;"
        @click="channelStore.changeChannelId(v.id)"
      >
        {{ v.name }}
      </a>
    </nav>
  </div>
</template>
